import turbogears as tg
import cherrypy

from creoleparser.dialects import create_dialect, creole11_base
from creoleparser.core import Parser

wiki_base_url = tg.config.get('turbowiki.wiki_base_url', '/wiki')

params = dict(
    wiki_links_base_url=wiki_base_url+'/view/',
    interwiki_links_base_urls=dict(wikicreole='/interwiki/view/'))

wiki_parser = Parser(dialect=create_dialect(creole11_base, **params))
wiki_validator = tg.validators.Regex(r'[A-z0-9_.]',
    not_empty=True, max=64, strip=True)

def get_user_ip():
    '''Return the ip of the visitor.'''
    headers = cherrypy.request.headers
    ip = headers.get('X-FORWARDED-FOR', headers['Remote-Addr'])
    return ip

def get_user_id():
    '''If the user is logged return the id of the user, if the
    user is anonymous return None.
    '''
    if tg.identity.current.anonymous:
        user_id = None
    else:
        user_id = tg.identity.current.user.user_id
    return user_id

def get_locale():
    locale = tg.i18n.utils.get_locale()
    if '_' in locale:
        locale = locale.split('_')[0]
    #FIXME: i18n, next time
    return 'en'
    return locale
